package cn.gwm.flink.streaming.dwd.check;

import cn.gwm.flink.streaming.constant.BaseFields;
import cn.gwm.flink.streaming.dwd.model.DwdErrorEnum;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONObject;
import lombok.Data;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;

import java.io.Serializable;

/**
 * @ClassName DwdCheck
 * @Description
 * @Author LiangGuang
 * @Date 2023/05/18 13:17
 */
@Slf4j
@Data
@Accessors(chain = true)
public class DwdCheck implements Serializable {

    public int signalNum;

    public int vinLen = 17;

    public static int TIME_RANGE = 200;

    public String vehicleType;

    private static final DwdCheck DWD_CHECK = new DwdCheck();

    /**
     * 直接返回一个 ，省得再次生成，其中 车型和 字段范围版本 再次赋值即可，类似享元
     *
     * @return
     */
    public static DwdCheck getDwdCheck() {
        return DWD_CHECK;
    }


    public void check(JSONObject source) {
        // 信号量判断，此处忽略
        // deviceId 长度
        // 时间范围 ，默认200小时
        keyCheck(source);
        extra(source);
    }

    public void signalAmountCheck(JSONObject source) {
        if (source.size() != signalNum) {
            source.set(BaseFields.ERROR, DwdErrorEnum.ERROR_SIGNAL_NUM.getCode());
        }
    }

    public void keyCheck(JSONObject source) {
        if (!source.containsKey(BaseFields.ERROR)) {
            if (source.containsKey(BaseFields.vin)) {
                if (source.getStr(BaseFields.vin).length() != vinLen) {
                    source.set(BaseFields.ERROR, DwdErrorEnum.ERROR_VIN.getCode());
                }
            } else {
                source.set(BaseFields.ERROR, DwdErrorEnum.ERROR_VIN_LACK.getCode());
                source.set(BaseFields.vin, BaseFields.defaultVal);
                return;
            }
            if (source.containsKey(BaseFields.tid)) {
                if (!checkTime(source)) {
                    source.set(BaseFields.ERROR, DwdErrorEnum.ERROR_TID.getCode());
                }
            } else {
                source.set(BaseFields.ERROR, DwdErrorEnum.ERROR_VIN_LACK.getCode());
                source.set(BaseFields.tid, BaseFields.defaultVal);
            }

        }

    }

    /**
     * 额外的字段处理 预留
     *
     * @param source
     */
    public void extra(JSONObject source) {

    }


    public static boolean checkTime(JSONObject source) {

        try {
            Long time = source.getLong(BaseFields.tid);
            if (time != null && time != 0) {
                // 200 小时之内  todo  补录数据 放开
//                return DateUtil.date(time).offset(DateField.HOUR, TIME_RANGE).after(DateUtil.date());
                DateUtil.date(time);
                return true;
            }
        } catch (Exception e) {
            log.error("checkTime happen error." + e.getMessage() + "; deviceId is " + source.getStr(BaseFields.vin) + "; item_time is " + source.get(BaseFields.tid));
            return false;
        }
        return false;
    }


}
